import { getCategoryByTypeAPI } from "@/api/business/category"
import { CategoryData } from "@/types/category"
import { AxiosError } from "axios"
import { useUserStore } from "@/stores/userStore"
import { ElMessage } from "element-plus"
import { ref } from "vue"
import { getAdminCategoryByTypeAPI } from "@/api/admin/category"

export default function () {
  //根据类型查询分类列表
  const categoryList = ref<CategoryData[]>([])
  const userStore = useUserStore()
  async function getCategorysByType(type?: number) {
    try {
      if (userStore.rule === 'admin') {
        let { data } = await getAdminCategoryByTypeAPI()
        categoryList.value = data
      } else {
        const businessId = userStore.user.id
        let { data } = await getCategoryByTypeAPI(businessId, type)
        categoryList.value = data
      }
    } catch (error) {
      const err = <AxiosError>error
      ElMessage.error(err)
    }
  }
  return {
    categoryList,
    getCategorysByType
  }
}